//
//  GWDetailStatuseTableViewController.m
//  新浪微博
//
//  Created by qianfeng on 16/2/25.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWDetailStatuseTableViewController.h"
#import "GWStatuseDetailView.h"//
#import "GWDetailStatuseFrame.h"
#import "GWStatuseModel.h"
#import "GWStatuseToolBar.h"
#import "GWDetailTopToolBar.h"
#import "GWCommentModel.h"
#import "GWStatuseTool.h"

@interface GWDetailStatuseTableViewController ()<UITableViewDataSource,UITableViewDelegate,GWDetailTopToolBarDelegate>
@property (nonatomic,weak)GWStatuseDetailView *detailView;
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)GWDetailTopToolBar *topToolBar;

@property (nonatomic,copy)NSMutableArray *dataSource;
@end

@implementation GWDetailStatuseTableViewController
-(NSMutableArray *)dataSource
{
    if (_dataSource==nil) {
        _dataSource =[NSMutableArray array];
    }
    return _dataSource;
}
-(void)detailTopToolBar:(GWDetailTopToolBar *)topToolBar didSelecteBtnType:(GWDetailTopToolBarBtnType)btntype
{
    switch (btntype) {
        case GWDetailTopToolBarBtnTypeReport:
            
            break;
        case GWDetailTopToolBarBtnTypeComment:
            [self loadComment];
            break;
        case GWDetailTopToolBarBtnTypeLike:
            
            break;
    }
}
-(void)loadComment
{
    GWCommentParam *parma =[GWCommentParam param];
    parma.id =self.statuse.idstr;
    GWCommentModel *comment =[self.dataSource firstObject];
    parma.since_id =comment.idstr;
    [GWStatuseTool detailCommentWithParame:parma success:^(GWCommentParamResult *result) {
        self.statuse.comments_count =[result.total_number intValue];
         self.topToolBar.statuse =self.statuse;
        NSIndexSet *set =[[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(0, result.comments.count)];
        [self.dataSource insertObjects:result.comments atIndexes:set];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
-(void)loadReport
{
    
}
-(GWDetailTopToolBar *)topToolBar
{
    if (_topToolBar==nil) {
        _topToolBar=[[GWDetailTopToolBar alloc]init];
    
    }
    return _topToolBar;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"微博正文";
    
    
    [self setupTableView];
    
    [self setupStatuseDetailView];
    
    [self setupBottomToolBar];
    
    self.topToolBar.delegate =self;
    self.topToolBar.statuse =self.statuse;
    
    
}
-(void)setupBottomToolBar
{
    GWStatuseToolBar *toolBar =[[GWStatuseToolBar alloc]init];
    toolBar.frame =CGRectMake(0, self.tableView.height, GWScreenW, 35);
    [self.view addSubview:toolBar];
    toolBar.backgroundColor =[UIColor whiteColor];
}
-(void)setupTableView
{
    UITableView *tableView =[[UITableView alloc]init];
    tableView.width =GWScreenW;
    tableView.height =self.view.height -35;
    [self.view addSubview:tableView];
    tableView.separatorStyle =UITableViewCellEditingStyleNone;
    self.tableView =tableView;
    self.tableView.backgroundColor =GWGlobalBackgroundColor;
    tableView.delegate=self;
    tableView.dataSource =self;
}
-(void)setupStatuseDetailView
{
    GWStatuseDetailView *detailView =[[GWStatuseDetailView alloc]init];
    GWDetailStatuseFrame *frame =[[GWDetailStatuseFrame alloc]init];
    self.statuse.retweeted_status.detail =YES;
    frame.statuse =self.statuse;
    detailView.detailStatuseFrame =frame;
    detailView.height =frame.frame.size.height;
    self.tableView.tableHeaderView =detailView;
    self.detailView =detailView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GWCommentModel *comment =self.dataSource[indexPath.row];
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text=comment.text;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.topToolBar;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

@end
